<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>学习mongodb的基础用法 增删改查 + 聚合查询 | 技术笔记</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="mongodb的介绍1. MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。 2. MongoDB 是一个介于关系数据库和非关系数据库之间的产品，是非关系数据库当中功能最丰富，最像关系数据库的。mongodb的安装[mongodb的安装](https:&#x2F;&#x2F;docs.mongodb.com&#x2F;)mongodb的语法（简单介绍">
<meta property="og:type" content="article">
<meta property="og:title" content="学习mongodb的基础用法 增删改查 + 聚合查询">
<meta property="og:url" content="http://wben107.gitee.io/hexo-blog/2020/05/24/nodejs/%E5%AD%A6%E4%B9%A0mongodb%E7%9A%84%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95%20%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5+%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2/index.html">
<meta property="og:site_name" content="技术笔记">
<meta property="og:description" content="mongodb的介绍1. MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。 2. MongoDB 是一个介于关系数据库和非关系数据库之间的产品，是非关系数据库当中功能最丰富，最像关系数据库的。mongodb的安装[mongodb的安装](https:&#x2F;&#x2F;docs.mongodb.com&#x2F;)mongodb的语法（简单介绍">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-23T22:59:32.000Z">
<meta property="article:modified_time" content="2020-05-23T23:18:42.544Z">
<meta property="article:author" content="biner">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo-blog/atom.xml" title="技术笔记" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/hexo-blog/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo-blog/" id="logo">技术笔记</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/hexo-blog/" id="subtitle">技术笔记</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo-blog/">Home</a>
        
          <a class="main-nav-link" href="/hexo-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://wben107.gitee.io/hexo-blog"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-nodejs/学习mongodb的基础用法 增删改查+聚合查询" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/hexo-blog/2020/05/24/nodejs/%E5%AD%A6%E4%B9%A0mongodb%E7%9A%84%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95%20%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5+%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2/" class="article-date">
  <time datetime="2020-05-23T22:59:32.000Z" itemprop="datePublished">2020-05-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      学习mongodb的基础用法 增删改查 + 聚合查询
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="mongodb的介绍"><a href="#mongodb的介绍" class="headerlink" title="mongodb的介绍"></a>mongodb的介绍</h3><pre><code>1. MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
2. MongoDB 是一个介于关系数据库和非关系数据库之间的产品，是非关系数据库当中功能最丰富，最像关系数据库的。</code></pre><h3 id="mongodb的安装"><a href="#mongodb的安装" class="headerlink" title="mongodb的安装"></a>mongodb的安装</h3><pre><code>[mongodb的安装](https://docs.mongodb.com/)</code></pre><h3 id="mongodb的语法（简单介绍-具体的还得去官网查询）"><a href="#mongodb的语法（简单介绍-具体的还得去官网查询）" class="headerlink" title="mongodb的语法（简单介绍 具体的还得去官网查询）"></a>mongodb的语法（简单介绍 具体的还得去官网查询）</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">mongo</span><br><span class="line"><span class="meta">&gt;</span><span class="bash"> use users; // 选择数据库</span></span><br><span class="line"><span class="meta">&gt;</span><span class="bash"> db.users.find([,filter,projection])</span></span><br><span class="line">  </span><br><span class="line"><span class="meta">&gt;</span><span class="bash"> db.users.insert(&#123;name:<span class="string">'小明'</span>,age:<span class="string">'12'</span>&#125;) //插入</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;</span><span class="bash"> db.users.update(&#123;name:<span class="string">'小明'</span>&#125;,&#123;<span class="variable">$set</span>:&#123;name:<span class="string">"小红"</span>&#125;&#125;)  // 带$的既为操作符号</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;</span><span class="bash"> db.users.delete(&#123;age:<span class="string">'12'</span>&#125;,&#123;muilt:<span class="literal">true</span>&#125;) //删除多个</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 聚合查询 aggregation</span><br><span class="line"></span><br><span class="line">db.aggregate(</span><br><span class="line">    &#123;</span><br><span class="line">        $match:&#123;   //这里代表的是过滤一样的</span><br><span class="line">            $exists:&#123;</span><br><span class="line">                "name":1 </span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">       $group:&#123;</span><br><span class="line">           "_id":null,</span><br><span class="line">           "total":&#123;</span><br><span class="line">               $sum:"$age"</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://wben107.gitee.io/hexo-blog/2020/05/24/nodejs/%E5%AD%A6%E4%B9%A0mongodb%E7%9A%84%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95%20%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5+%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2/" data-id="ckak98k5b00083ms6g17benfv" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/hexo-blog/2020/05/23/nodejs/nodejs%E7%9A%84mvc(2)/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">nodejs的mvc</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/hexo-blog/categories/coding/">coding</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo-blog/tags/javascript/" rel="tag">javascript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo-blog/tags/nodejs/" rel="tag">nodejs</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/hexo-blog/tags/javascript/" style="font-size: 10px;">javascript</a> <a href="/hexo-blog/tags/nodejs/" style="font-size: 10px;">nodejs</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo-blog/archives/2020/05/">五月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo-blog/2020/05/24/nodejs/%E5%AD%A6%E4%B9%A0mongodb%E7%9A%84%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95%20%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5+%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2/">学习mongodb的基础用法 增删改查 + 聚合查询</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/23/nodejs/nodejs%E7%9A%84mvc(2)/">nodejs的mvc</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/18/nodejs/nodejs%E8%AE%B2%E8%A7%A3%EF%BC%881%EF%BC%89/">nodejs讲解（1）</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/17/%E5%88%9D%E6%AC%A1%E6%8E%A5%E8%A7%A6hexo-%E5%AD%A6%E4%B9%A0%E6%AD%A5%E9%AA%A4/">初次接触hexo --学习步骤</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/17/my-first-article/">my first article</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 biner<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/hexo-blog/fancybox/jquery.fancybox.css">

  
<script src="/hexo-blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/hexo-blog/js/script.js"></script>




  </div>
</body>
</html>